<?php

namespace app\wxapi\controller;

use Endroid\QrCode\QrCode;
use think\Request;
use think\Response;
use think\Session;
use EasyWeChat\Payment\Order as WxOrder;
use Carbon\Carbon;

class Order extends Api
{
    /**
     * 订单列表
     * @return [type] [description]
     */
    public function getIndex()
    {
        $order_type = $this->request->get('order_type', 'order');
        if ($orders = $this->getOrderList($order_type)) {
            return view('index', ['orders' => $orders]);
        }
    }
    public function getShow()
    {
        $order_id    = $this->request->get('order_id');
        $order_type = $this->request->get('order_type', 'order');
        $open_id    = cookie('auth_id');

        if ($order_info = $this->getOrderInfo($order_id, $order_type)) {
            switch ($order_type) {
                case 'recharge':
                    $order_info->number       = $order_info->order_num;
                    $order_info->total_amount = $order_info->cash;
                    break;
            }
            if ($order_info->status == 100 || $order_info->status == 0) {
                $attributes = [
                    'attach'           => $order_type,
                    'trade_type'       => 'JSAPI', // JSAPI，NATIVE，APP...
                    'body'             => '双辉盛业订单',
                    'detail'           => '双辉盛业订单:' . $order_info->number,
                    'out_trade_no'     => $order_info->number,
                    'total_fee'        => 1,//$order_info->total_amount,
                    'notify_url'       => 'http://wx.shsytour.cn/wxpay/notify.html', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
                    'openid'           => $open_id,
                ];

                $payment = $this->app->payment;
                $js      = $this->app->js;
                $order   = new WxOrder($attributes);

                $result = $payment->prepare($order);

                if ($result->return_code == 'SUCCESS' && $result->err_code == 'INVALID_REQUEST') {
                    return view('ordershow', ['order' => $order_info]);
                }

                if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
                    $prepayId = $result->prepay_id;
                    $p        = $payment->configForJSSDKPayment($prepayId);
                    return view('order', [
                            'order'  => $order_info, 
                            'config' => $js->config(array('chooseWXPay')), 
                            'p'      => $p
                        ]);
                }
            }else{
                return view('ordershow', ['order' => $order_info]);
            }
        }   
    }
    public function getOrder()
    {
           
    }
    private function getOrderInfo($order_id, $order_type = 'order')
    {
        switch ($order_type) {
            case 'order':
                return model('Orders')->getOrderInfo(['number' => $order_id]);
                break;
            case 'recharge':
                return model('IntergrateOrders')->getOrderInfo(['order_num' => $order_id]);
                break;
        }
        return null;
    }
    private function getOrderList($order_type = 'order')
    {
        switch ($order_type) {
            case 'order':
                return model('Orders')->getOrders();
                break;
            case 'recharge':
                //return model('IntergrateOrders')->getOrderInfo(['order_num' => $order_id]);
                break;
        }
        return null;
    }
    public function getDoRecharge()
    {
        $attributes = [
            'attach'           => 'recharge',
            'trade_type'       => 'JSAPI', // JSAPI，NATIVE，APP...
            'body'             => '双辉盛业订单',
            'detail'           => '双辉盛业订单:',
            'out_trade_no'     => '3121321321',
            'total_fee'        => $this->request->get('total'),
            'notify_url'       => 'http://wx.shsytour.cn/wxpay/notify.html', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'openid'           => $this->open_id,
        ];

        $payment = $this->app->payment;
        $js      = $this->app->js;
        $order   = new WxOrder($attributes);

        $result = $payment->prepare($order);

        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
            $prepayId = $result->prepay_id;
            $p        = $payment->configForJSSDKPayment($prepayId);
            return json(['code' => 100, 'data' => $p]);
        }
        return json(['code' => 200, 'msg' => '']);
    }
}
